using Services;
using UnityEngine;
using UnityEngine.UI;

public class ContainerUI_PlayerMoney : MonoBehaviour
{
    private Text[] m_texts;
    private LinearTransformation linearTransformation;

    private void Start()
    {
        m_texts = GetComponentsInChildren<Text>();
        ServiceLocator.Get<ActionController>().player.MoneyChange += OnMoneyChange;
        linearTransformation = new LinearTransformation();
        linearTransformation.Tick += SetAlpha;
    }

    private void OnMoneyChange(int value, int delta)
    {
        m_texts[0].text = $"{value}g";
        if (delta != 0)
        {
            if (delta > 0)
            {
                m_texts[1].text = $"+ {delta}g";
                m_texts[1].color = Color.green;
            }
            else
            {
                m_texts[1].text = $"- {-delta}g";
                m_texts[1].color = Color.red;
            }
            linearTransformation.Initialize(1, 0, 0.6f);
        }
    }

    private void SetAlpha(float value)
    {
        m_texts[1].color = m_texts[1].color.SetAlpha(value);
    }
}